package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.wiz.weixin.TO.StationTO;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.mapper.StationMapper;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.util.PageView;
@Service("stationService")
public class StationServiceImpl implements StationService {

	@Resource
	private StationMapper stationMapper;
	@Resource
	private HttpManager httpManager;
	@Override
	public List<StationTO> getStation(String stationId) {
		return null;
	}

	@Override
	public PageView stationsByHq(List<String>hqIds,double lat,double lng,PageView pageView) {
		List<StationTO> list = new ArrayList<StationTO>();
		if(hqIds == null || hqIds.size()<=0){
			pageView.setRecords(new ArrayList<StationTO>());
			return pageView;
		}
		Map<String, Object>map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("hqIds",hqIds );
		map.put("lat",lat);
		map.put("lng",lng);
		list = stationMapper.query(map);
		pageView.setRecords(list);
		return pageView;
	}
	@Override
	public List<String> getHQIds(String openid) throws Exception{
		List<String>hqIds = new ArrayList<String>();
		Map<String,String> params = new HashMap<String,String>();
		params.put("openId", openid);
		String result = httpManager.getHttpsResponseByPost(Constant.QUERY_MEMBER_URL, params, "utf-8");
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				JSONArray list = data.getJSONArray("list");
				for(int i = 0;i<list.length();i++){
					hqIds.add(list.getJSONObject(i).getString("HQId"));
					System.out.println(list.getJSONObject(i).getString("HQId"));
				}
				
			}
		}
       return hqIds;
	}

}
